# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Jeff Bilyk <jbilyk@gmail.com>
pkgname=cacti
pkgver=1.2.25
pkgrel=0
pkgdesc="The complete rrdtool-based graphing solution"
url="https://www.cacti.net/"
arch="noarch"
license="GPL-2.0-or-later"
options="!check"
pkgusers=$pkgname
pkggroups="www-data"
depends="bash perl coreutils net-snmp-tools rrdtool font-dejavu"
subpackages="$pkgname-doc $pkgname-lang $pkgname-setup $pkgname-php:_php"
install="$pkgname.pre-install $pkgname-setup.post-install"
source="cacti-src-$pkgver.tar.gz::https://www.cacti.net/downloads/cacti-$pkgver.tar.gz
	$pkgname.crontab
	$pkgname.nginx.conf
	$pkgname.php-fpm.conf
	"

# secfixes:
#   1.2.25-r0:
#     - CVE-2023-30534
#     - CVE-2023-39360
#     - CVE-2023-39361
#     - CVE-2023-39357
#     - CVE-2023-39362
#     - CVE-2023-39359
#     - CVE-2023-39358
#     - CVE-2023-39365
#     - CVE-2023-39364
#     - CVE-2023-39366
#     - CVE-2023-39510
#     - CVE-2023-39511
#     - CVE-2023-39512
#     - CVE-2023-39513
#     - CVE-2023-39514
#     - CVE-2023-39515
#     - CVE-2023-39516
#   1.2.20-r0:
#     - CVE-2022-0730
#   1.2.17-r0:
#     - CVE-2020-35701
#   1.2.13-r0:
#     - CVE-2020-11022
#     - CVE-2020-11023
#     - CVE-2020-13625
#     - CVE-2020-14295
#   1.2.8-r0:
#     - CVE-2020-8813
#     - CVE-2020-7237
#     - CVE-2020-7106

package() {
	mkdir -p "$pkgdir"/usr/share/webapps/$pkgname \
		"$pkgdir"/var/lib/$pkgname \
		"$pkgdir"/etc/$pkgname \
		"$pkgdir"/var/log

	cp -r ./* "$pkgdir"/usr/share/webapps/$pkgname
	install -Dm600 "$srcdir"/$pkgname.crontab "$pkgdir"/etc/crontabs/$pkgname

	# fix permissions
	find "$pkgdir" -perm 755 -type f -exec chmod -x {} \;

	# remove development data
	rm -fr "$pkgdir"/usr/share/webapps/$pkgname/locales/po \
		"$pkgdir"/usr/share/webapps/$pkgname/locales/update-pot.sh

	# switch to system fonts
	rm -fr "$pkgdir"/usr/share/webapps/$pkgname/include/fonts
	ln -s /usr/share/fonts/dejavu \
		"$pkgdir"/var/lib/$pkgname/fonts
	ln -s /var/lib/$pkgname/fonts \
		"$pkgdir"/usr/share/webapps/$pkgname/include/fonts

	# symlink to help webserver find relative path
	ln -s . "$pkgdir"/usr/share/webapps/$pkgname/$pkgname

	# move variable and writable data to cacti's home
	local dir; for dir in cache plugins resource rra scripts; do
		mv "$pkgdir"/usr/share/webapps/$pkgname/$dir \
			"$pkgdir"/var/lib/$pkgname/$dir
		chown -R $pkgname: "$pkgdir"/var/lib/$pkgname/$dir
		ln -s /var/lib/$pkgname/$dir "$pkgdir"/usr/share/webapps/$pkgname/$dir
	done

	# move log to /var/log
	mv "$pkgdir"/usr/share/webapps/$pkgname/log \
		"$pkgdir"/var/log/$pkgname
	chown -R $pkgname: "$pkgdir"/var/log/$pkgname
	ln -s /var/log/$pkgname "$pkgdir"/usr/share/webapps/$pkgname/log

	# move config to /etc
	mv "$pkgdir"/usr/share/webapps/$pkgname/include/config.php \
		"$pkgdir"/etc/$pkgname/config.php
	ln -s /etc/$pkgname/config.php \
		"$pkgdir"/usr/share/webapps/$pkgname/include/config.php
}

doc() {
	default_doc

	mkdir -p "$subpkgdir"/usr/share/webapps/$pkgname
	cd "$pkgdir"/usr/share/webapps/$pkgname
	mv docs "$subpkgdir"/usr/share/webapps/$pkgname/

	local file;
	find ./ -name "LICENSE" -o -name "NEWS" \
		 -o -name "README*" -o -name "VERSION" \
		 -o -name "CHANGELOG" -o -name "*.rst" \
	| while read -r file; do
		mkdir -p "$subpkgdir"/usr/share/webapps/$pkgname/${file%/*}
		mv $file "$subpkgdir"/usr/share/webapps/$pkgname/$file
	done

	install -Dm644 "$srcdir"/$pkgname.nginx.conf \
		"$subpkgdir"/usr/share/webapps/$pkgname/docs/txt/nginx.conf
}

lang() {
	pkgdesc="$pkgdesc (localisations)"
	install_if="lang $pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/share/webapps/$pkgname/locales/LC_MESSAGES \
		"$subpkgdir"/usr/share/webapps/$pkgname/include/js/LC_MESSAGES \
		"$subpkgdir"/usr/share/webapps/$pkgname/include/vendor/phpmailer/language

	# cacti l10n
	mv "$pkgdir"/usr/share/webapps/$pkgname/locales/LC_MESSAGES/* \
		"$subpkgdir"/usr/share/webapps/$pkgname/locales/LC_MESSAGES

	# jquery l10n
	mv "$pkgdir"/usr/share/webapps/$pkgname/include/js/LC_MESSAGES/* \
		"$subpkgdir"/usr/share/webapps/$pkgname/include/js/LC_MESSAGES

	# phpmailer l10n
	mv "$pkgdir"/usr/share/webapps/$pkgname/include/vendor/phpmailer/language/*lang*.php \
		"$subpkgdir"/usr/share/webapps/$pkgname/include/vendor/phpmailer/language
}

setup() {
	pkgdesc="$pkgdesc (initial setup)"
	depends="$pkgname $pkgname-doc mariadb-client"

	mkdir -p "$subpkgdir"/usr/share/webapps/$pkgname
	mv "$pkgdir"/usr/share/webapps/$pkgname/install \
		"$pkgdir"/usr/share/webapps/$pkgname/*.sql \
		"$subpkgdir"/usr/share/webapps/$pkgname/
}

_php() {
	local php=php82
	pkgdesc="$pkgdesc ($php dependencies)"
	install_if="$php-config $pkgname=$pkgver-r$pkgrel"
	depends="$php $php-gd $php-gmp $php-ldap $php-mbstring $php-pdo_mysql $php-opcache
		$php-openssl $php-posix $php-session $php-simplexml $php-snmp $php-sockets
		$php-xml $php-fpm"
	provides="cacti-php81=$pkgver-r$pkgrel" # for backward compatibility
	replaces="cacti-php81" # for backward compatibility

	# cacti's php-fpm pool config
	install -Dm644 "$srcdir"/$pkgname.php-fpm.conf \
		"$subpkgdir"/etc/$php/php-fpm.d/$pkgname.conf
	amove etc/crontabs/$pkgname
}

sha512sums="
15dec723102343b4457ef2baeb98789125c69cceb52f6baac1d002dde42e844da5189fd23f77fe1e72f0e80ee6d329d0df653af18e1f624012f00fbaaee62f34  cacti-src-1.2.25.tar.gz
2ff197a75e366b4fdb2f651643a08b5b14140225edd5aea673cfbedeb92aab0b4625c789896990cc023e35fb20c2370c917253c92f51568b293f39452f1a6b74  cacti.crontab
9b3fe765c6196c0e4988efaa7236d8a8b945725548371b4a0e2a371de374c9577a908d58dcef5a4e59e089ca923cfeb7c5ddea9ee983a5115239052cf9b8ab59  cacti.nginx.conf
056358fc69752fb5129729db91a22d06c97ca452068017ccfe0ede8f2bf42f62e5072415c7db2eb5b9346d6bc54092bc147044e40b2156d8037dfee4a7e55e5b  cacti.php-fpm.conf
"
